﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.BBS;
using DFGK.IService.BBS;
using DFGK_WMS.Model.Models.BBS;

namespace DFGK.Service.BBS
{
    public class MaterialInfoService : BaseService, IMaterialInfoService
    {
        private readonly IMaterialInfoRepository _materialInfoRepo;
        private readonly IUnitOfWork _unitOfWork;
        public MaterialInfoService(IMapper mapper, IMaterialInfoRepository materialInfoRepo, IUnitOfWork unitOfWork) : base(mapper)
        {
            _materialInfoRepo = materialInfoRepo;
            _unitOfWork = unitOfWork;
        }

        public List<MaterilaInfo> GetPageData(string? infoName, int pageIndex, int pageSize, out int totalCount)
        {
            var query = _materialInfoRepo.GetEntityAll();
            if (!string.IsNullOrEmpty(infoName))
            {
                query = query.Where(t => t.InfoName.Contains(infoName));
            }
            
            totalCount = query.Count();
            query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(o => o.Id);
            return query.ToList();
        }

        public bool SubmitMaterialInfo(MaterilaInfo  materilaInfo, out string msg)
        {
            msg = "";
            if (materilaInfo.Id > 0)
            {
                _materialInfoRepo.UpdEntity(materilaInfo);
            }
            else
            {
                _materialInfoRepo.AddEntity(materilaInfo);
            }
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelMaterialInfo(long materialInfoId, out string msg)
        {
            msg = "";
            var post = _materialInfoRepo.QueryEntityById(materialInfoId);
            if (post == null)
            {
                msg = "参数错误";
                return false;
            }
            _materialInfoRepo.DelEntity(post);
            return _unitOfWork.SaveChangesEntity();
        }
        public MaterilaInfo? GetMaterialInfo(long materialInfoId)
        {
            return _materialInfoRepo.QueryEntityById(materialInfoId);
        }
    }
}
